//
//  UPActivityProductListView.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/21.
//

#import "UPActivityProductListView.h"
#import "UPProductListCell.h"

@interface UPActivityProductListView () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation UPActivityProductListView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.tableView];
    [self setupConstraints];
}

- (void)setupConstraints {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

#pragma mark - Lazy 加载组件

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.scrollEnabled = NO;
        _tableView.estimatedRowHeight = 160;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        [_tableView registerClass:[UPProductListCell class] forCellReuseIdentifier:@"UPProductListCell"];
    }
    return _tableView;
}

#pragma mark - Public Methods

- (void)setProductList:(NSArray<LeaderibleModel *> *)productList {
    _productList = productList;
    [self.tableView reloadData];
    
    CGFloat totalHeight = [self getTableViewListHeight];
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(totalHeight));
    }];
}

- (void)reloadData {
    [self.tableView reloadData];
}

- (CGFloat)getTableViewListHeight {
    return ([self cellHeight] + 15) * (CGFloat)self.productList.count;
}

- (CGFloat)cellHeight {
    UIImage *image = [UIImage imageNamed:@"product_cell_bg"];
    CGFloat width = kScreenWidth - 2 * 16;
    CGFloat ratio = 1.0;
    if (image.size.width > 0 && image.size.height > 0) {
        ratio = image.size.width / image.size.height;
    }
    CGFloat height = width / ratio;
    return height;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.productList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UPProductListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UPProductListCell" forIndexPath:indexPath];
    
    if (indexPath.row >= self.productList.count) {
        return cell;
    }
    
    LeaderibleModel *product = self.productList[indexPath.section];
    [cell configureWithProduct:product];
    
    __weak typeof(self) weakSelf = self;
    cell.applyButtonBlock = ^(LeaderibleModel *product) {
        if ([weakSelf.delegate respondsToSelector:@selector(productListViewDidSelectProduct:)]) {
            [weakSelf.delegate productListViewDidSelectProduct:product];
        }
    };
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < self.productList.count) {
        LeaderibleModel *product = self.productList[indexPath.section];
        if ([self.delegate respondsToSelector:@selector(productListViewDidSelectProduct:)]) {
            [self.delegate productListViewDidSelectProduct:product];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self cellHeight];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 15;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor clearColor];
    return footerView;
}

@end
